<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px"
				 label-position="left">
			<el-form-item label="职位名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入职位名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注信息" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注信息" clearable></el-input>
			</el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" placeholder="请输入排序" clearable></el-input>
            </el-form-item>
			<el-form-item label="状态" prop="status">
				<el-switch v-model="form.status" :active-value="true" :inactive-value="false"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
// import roleSelect from "@/components/system/roleSelect";
export default {
	emits: ['success', 'closed', 'reloadData'],
	// components:{roleSelect},
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增职位',
				edit: '修改职位信息',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				name: "",//职位名称
				remark:"",
                status: true,//状态
                sort: 0//排序
			},
			// 修改密码
			changePassword: '',
			//验证规则
			rules: {
				name: [
					{required: true, message: '请输入职位名称'}
				],
				status: [
					{required: true}
				],
			}
		}
	},
	mounted() {

	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					let res;
					if (this.mode === 'add') {
						res = await this.$API.company.position.add.post(this.form);
					} else if (this.mode === 'edit') {
						res = await this.$API.company.position.edit.post(this.form);
					}
					this.isSaveing = false;
					if (res.code === 0) {
						this.$emit('reloadData')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		setData(data) {
			this.form.id = data.id
			this.form.name = data.name
            this.form.remark = data.remark
            this.form.sort = data.sort
			this.form.status = data.status === 1 ? true : false
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			//Object.assign(this.form, data)
		}
	}
}
</script>

<style>
</style>
